package com.aitoedu.server.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.aitoedu.server.dto.PreviewDTO;
import com.aitoedu.server.mapper.PreviewStatusMapper;
import com.aitoedu.server.mapper.ScMapper;
import com.aitoedu.server.vo.ChapterPreviewVO;
import com.aitoedu.server.vo.CoursePreviewVO;
import com.aitoedu.server.vo.PreviewVO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aitoedu.server.entity.Preview;
import com.aitoedu.server.service.PreviewService;
import com.aitoedu.server.mapper.PreviewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 曹正豪
* @description 针对表【Preview(预习表)】的数据库操作Service实现
* @createDate 2024-10-17 22:51:15
*/
@Service
public class PreviewServiceImpl extends ServiceImpl<PreviewMapper, Preview>
    implements PreviewService{

    @Autowired
    private PreviewMapper previewMapper;

    @Autowired
    private PreviewStatusMapper previewStatusMapper;

    @Autowired
    private ScMapper scMapper;

    @Override
    public void pushPreview(PreviewDTO previewDTO) {
        Preview preview = Preview.builder()
                .createTime(LocalDateTime.now())
                .chapterId(previewDTO.getChapterId())
                .content(previewDTO.getContent())
                .courseId(previewDTO.getCourseId())
                .build();
        previewMapper.insert(preview);
        List<Long> sids = scMapper.selectSidByCourseId(previewDTO.getCourseId());
        previewStatusMapper.insertAllStudent(sids,preview.getId());
    }

    @Override
    public List<PreviewVO> preview(Long chapterId) {
        Long sid = Long.valueOf(StpUtil.getLoginId().toString());
        List<PreviewVO> contents = previewStatusMapper.selectAllContent(chapterId,sid);
        return contents;
    }

    @Override
    public List<CoursePreviewVO> previewList() {
        Long sid = Long.valueOf(StpUtil.getLoginId().toString());
        List<CoursePreviewVO> coursePreviewVOList = previewMapper.getCidsNameBySid(sid);
        for(CoursePreviewVO c:coursePreviewVOList){
            List<ChapterPreviewVO> chapterPreviewVOList = previewMapper.myPreview(sid,c.getCourseId());
            for(ChapterPreviewVO cp:chapterPreviewVOList){
                List<PreviewVO> previewVOList = this.preview(cp.getChapterId());
                cp.setPreviewVOList(previewVOList);
            }
            c.setChapterPreviewVOList(chapterPreviewVOList);
        }
        return coursePreviewVOList;
    }
}




